package com.esunny.resourcevisualization.scheduled;

import com.esunny.resourcevisualization.domain.entity.Apply;
import com.esunny.resourcevisualization.manage.ApplyManage;
import com.esunny.resourcevisualization.manage.ResourceTypeManage;
import com.esunny.resourcevisualization.manage.SystemConfigManage;
import com.esunny.resourcevisualization.manage.UserManage;
import com.esunny.resourcevisualization.service.MessageService;
import com.esunny.resourcevisualization.util.StringAndDateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName SendUsingEndingMessage
 * @Description 发送即将到期通知
 * @Author baochencong
 * @Date 2021/1/13 13:08
 */

@Configuration
public class ApplyedResourceEndUsingCheck implements SchedulingConfigurer {

    @Resource
    private SystemConfigManage systemConfigManage;

    @Resource
    private ApplyManage applyManage;

    @Resource
    private MessageService messageService;

    @Resource
    private UserManage userManage;

    @Resource
    private ResourceTypeManage resourceTypeManage;

    @Value("${resourcevisualization.path.myApply}")
    private String myApplyPath;

    @Value("${resourcevisualization.path.auditApply}")
    private String auditApplyPath;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(this::task,
                triggerContext -> {
                    String cron = systemConfigManage.getConfigByKey("cron").getValue();
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                });
    }

    public void task() {
        System.out.println("定时任务开始执行");

        List<Apply> usingApplyList = applyManage.getUsingApplyList();
        for (Apply eachApply : usingApplyList) {
            if ("9999-12-31 12:00:00".equals(StringAndDateUtil.dateToString(eachApply.getApplyEndUseTime()))){
                continue;
            }
            int differentDays = differentOneDayByMillisecond(new Date(), eachApply.getApplyEndUseTime());
            String message = ",即将过期资源是id为"+eachApply.getApplyResourceId()+"的"+ resourceTypeManage.getResourceTypeByName(eachApply.getResourceTypeName()).getResourceTypeTitle();
            if (0 < differentDays && differentDays <= 3) {
                messageService.sendMessageToXuanXuan(eachApply.getApplyBy(), "您有一个资源申请即将过期，请及时处理"+message,myApplyPath);
            }
            if (differentDays == 0) {
                messageService.sendAdminMessageToXuanXuan(getUserRealName(eachApply.getApplyBy()) + "有一个即将过期的资源", "请及时回收"+message, auditApplyPath);
            }
        }

        System.out.println("定时任务执行结束");
    }

    /**
     * 通过用户名获取用户真实姓名
     *
     * @param username 用户名
     * @return 用户真实姓名
     */
    private String getUserRealName(String username) {
        return userManage.getUserByName(username).getRealName();
    }

    /**
     * 获取结束使用时间和当前差几天
     * @param currentDate 当前时间
     * @param targetDate 结束使用时间
     * @return 相差天数
     */
    public int differentOneDayByMillisecond(Date currentDate, Date targetDate) {
        return (int) ((targetDate.getTime() - currentDate.getTime()) / (1000 * 3600 * 24));
    }
}
